package com.locatead.proto.resource;

import java.util.Date;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.locatead.proto.model.Cell;
import com.locatead.proto.service.CellService;
import com.locatead.proto.util.wrapper.Response;

/*
 * Cell Resource
 * ==
 * GET
 *
 * POST
 * 
 * UPDATE
 * 
 * DELETE
 * 
 * 
 * @Author Eric Woojung Kim
 * @Date 2011 Jan 17 
 */
@Component
@Path("/cell")
public class CellResource {

	@Autowired
	private CellService service;

	@GET
	@Path("/echo/{msg}")
	@Produces("text/plain")
	public String echoMessage(@Context UriInfo uri, @PathParam("msg") String msg) {
		return this.service.wrap(msg);
	}
	
	@POST
	@Path("/test/")
	@Produces("text/plain")
	public String dbTest(@Context UriInfo uri) {
		return service.dbTest();
	}
	
	@GET
	@Path("/{lat}/{lng}")
	@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
	public Cell getCell(@Context UriInfo uri, @PathParam("lat") Integer x, @PathParam("lng") Integer y) {
		return service.getCell(x, y);
	}
	
	@PUT
	@Path("/{lat}/{lng}")
	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	public Response modifyCell(@Context UriInfo uri, @PathParam("lat") Integer x, @PathParam("lng") Integer y) {
		return null;
	}
	
	
	/*
	 * Testing Only
	 */
	
	@GET
	@Path("/dev/init/{id}")
	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	public Response initCellDB(
			@Context UriInfo uri, 
			@PathParam("id") String id,
			@HeaderParam("passwd") String passwd) {
		Response ret = new Response();
		
		try {
			service.initCellDB(id, passwd);
			ret.setResult(Response.OK);
			ret.setMessage("table \"CELL\" has been initialized for testing purpose");
		} catch (Exception e) {
			e.printStackTrace();
			ret.setResult(Response.FAILED);
			ret.setMessage("error in initializing table \"CELL\":" + e.getMessage());
		}
		
		return ret;
	}
	
	
}
